#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>


#define MAJOR 200
#define NAME "chrdevbase"

static char readbuf[100];
static char writebuf[100];
static char kerneldata[]={"kernel data!"};


static int chrdevbase_open(struct inode *inode, struct file *filp)
{
    printk("chrdevbase open!\n");
    return 0;
}

static int chrdevbase_release(struct inode *inode, struct file *filp)
{
    printk("chrdevbase released!\n");
    return 0;
}

static ssize_t chrdevbase_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
    memcpy(readbuf,kerneldata,sizeof(kerneldata));
    int ret=copy_to_user(buf,readbuf,cnt);
    if(ret==0){
        printk("kernel send-data ok!\n");
    }

    return 0;
}

static ssize_t chrdevbase_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
    int ret=copy_from_user(writebuf,buf,cnt);
    if(ret==0){
        printk("kernel received-data: %s\n", writebuf);
    }

    return 0;
}

static struct file_operations chrdevbase_fops={
    .owner=THIS_MODULE,
    .open=chrdevbase_open,
    .read=chrdevbase_read,
    .write=chrdevbase_write,
    .release=chrdevbase_release,
};

static int __init chrdevbase_init(void)
{
    int ret=register_chrdev(MAJOR, NAME, &chrdevbase_fops);
    if(ret>=0){
        printk("chrdevbase init()\n");
    }

    return 0;
}

static void __exit chrdevbase_exit(void)
{
    unregister_chrdev(MAJOR,NAME);
    printk("chrdevbase_exit()\n");
}

module_init(chrdevbase_init);
module_exit(chrdevbase_exit);

MODULE_AUTHOR("xujianchao");
MODULE_LICENSE("GPL");